#include <QDebug>
#include <QFileDialog>
#include "ConfigDialog.h"
#include "ui_ConfigDialog.h"
#include "ewha.h"
#include "SettingHelper.h"

ConfigDialog::ConfigDialog(QWidget *parent) :
	QDialog(parent),
	ui(new Ui::ConfigDialog)
{
	ui->setupUi(this);

	double h, v, z;
	bool b;
	QString diagnosisExePath, saveExt;
	QString consoleExePath;
	QString savePath;
	SettingHelper::loadCalibration(h, v);
	SettingHelper::loadZoomRatio(z);
	SettingHelper::loadFullScreen(b);
	SettingHelper::loadDiagnosis(&diagnosisExePath, &saveExt);
	SettingHelper::loadConsole(&consoleExePath);
	SettingHelper::loadImagePath(&savePath);
	ui->txtHorizontal->setText(QString::number(h));
	ui->txtVertical->setText(QString::number(v));
	ui->txtZoomRatio->setText(QString::number(z));
	ui->chkFullScreen->setChecked(b);
	ui->btnDiagnosisExePath->setText(diagnosisExePath);
	ui->txtSaveExt->setText(saveExt);
	ui->btnConsoleExePath->setText(consoleExePath);
	ui->btnSaveDir->setText(savePath);
}

ConfigDialog::~ConfigDialog()
{
	delete ui;
}

void ConfigDialog::accept()
{
	SettingHelper::saveCalibration(ui->txtHorizontal->text().toDouble(),
								   ui->txtVertical->text().toDouble());
	SettingHelper::saveZoomRatio(ui->txtZoomRatio->text().toDouble());
	SettingHelper::saveFullScreen(ui->chkFullScreen->isChecked());
	SettingHelper::saveDiagnosis(ui->btnDiagnosisExePath->text(), ui->txtSaveExt->text());
	SettingHelper::saveConsole(ui->btnConsoleExePath->text());
	SettingHelper::saveImagePath(ui->btnSaveDir->text());
	QDialog::accept();
}

void ConfigDialog::SlotSelectDiagnosisExePath()
{
	QString fileName = QFileDialog::getOpenFileName(this, tr("Select File"),
													QDir::homePath(),
													tr("Exe (*.exe)"));

	if(!fileName.isNull()) ui->btnDiagnosisExePath->setText(fileName);
}

void ConfigDialog::SlotSelectConsoleExePath()
{
	QString fileName = QFileDialog::getOpenFileName(this, tr("Select File"),
													QDir::homePath(),
													tr("Exe (*.exe)"));

	if(!fileName.isNull()) ui->btnConsoleExePath->setText(fileName);
}

void ConfigDialog::SlotSelectSavePath()
{
	QString dir = QFileDialog::getExistingDirectory(this, tr("Select Directory"),
													QDir::homePath(),
													QFileDialog::ShowDirsOnly
													| QFileDialog::DontResolveSymlinks);

	if(!dir.isNull()) ui->btnSaveDir->setText(dir);
}
